/* vuex模块化-channel模块 */
import { getChannelList, getAllChannelList } from '@/api/channels'
const state = {
  channelList: [],
  allChannelList: []
}
const mutations = {
  // 拿到我的频道
  setChannelList(state, payload) {
    state.channelList = payload
  },
  // 拿到所有频道
  setAllChannelList(state, payload) {
    state.allChannelList = payload
  },
  // 添加频道
  addChannel(state, channel) {
    state.channelList.push(channel)
  },
  // 删除频道
  delChannel(state, channel) {
    state.channelList = state.channelList.filter(item => +item.id !== +channel.id)
  }

}
const actions = {
  async getChannelList(context) {
    const res = await getChannelList()
    // console.log(res.data.channels)
    context.commit('setChannelList', res.data.channels)
  },
  async getAllChannelList(context) {
    const res = await getAllChannelList()
    // console.log(res.data.channels)
    context.commit('setAllChannelList', res.data.channels)
  }
}
const getters = {
  optionalChannelList(state) {
    return state.allChannelList
      .filter(item => state.channelList.findIndex(v => +v.id === +item.id) === -1)
  }
}
export default {
  state,
  mutations,
  getters,
  actions,
  namespaced: true
}
